//
//  OldViewController.m
//  Item A House
//
//  Created by dllo on 15/5/25.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "OldViewController.h"
#import "OldTableViewCell.h"
#import "OldModel.h"
#import "ConnectModel.h"
#import "UIImageView+WebCache.h"
#import "OldTwoViewController.h"
#import "PendulumView.h"
#import "UIScrollView+PullLoad.h"
@interface OldViewController ()<UITableViewDataSource, UITableViewDelegate, ConnectModelDelegate, PullDelegate>

@property (nonatomic, retain) ConnectModel *conn;

@property (nonatomic, retain) PendulumView *pendulum;
@end

@implementation OldViewController
- (void)dealloc
{
    _conn.delegate = nil;
    [_conn release];
    [_table release];
    [_hid release];
    [_array release];
    [_pendulum release];
    [super dealloc];
}
//@"http://m.goufang.com/api/ershou/eshonglist"
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
        self.count = 1;
        self.type = 1;
    }
    return self;
}



- (void)connectFinished:(NSData *)data
{
    NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//    NSLog(@"%@", dic);
    
   NSMutableArray *array = [dic objectForKey:@"Table"];
    for (NSMutableDictionary *dic in array) {
        OldModel *model = [[OldModel alloc]init];
        [model setValuesForKeysWithDictionary:dic];
        [self.array addObject:model];
    }
    [self.table reloadData];
    [self.pendulum removeFromSuperview];
    //根据计数type停止刷新或者下载
    if (self.type == 1) {
        [self.table stopLoadWithState:PullUpLoadState];
    }
    else {
        [self.table stopLoadWithState:PullDownLoadState];
    }
}
- (void)createTable
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    self.table.delegate = self;
    self.table.dataSource = self;
    self.table.pullDelegate = self;
    self.table.canPullDown = YES;
    self.table.canPullUp = YES;
    self.table.rowHeight = 100;
    [self.view addSubview:self.table];
    [self.table release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}
//tableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentify = @"李狗蛋";
    OldTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (!cell) {
        cell = [[[OldTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentify]autorelease];
    }
    OldModel *model = [self.array objectAtIndex:indexPath.row];
    
    [cell.housepic  sd_setImageWithURL:[NSURL URLWithString:model.housepic] placeholderImage:[UIImage imageNamed:@"10.jpg"]];
    cell.addrinfo.text = model.addrinfo;
    cell.areainfo.text = model.areainfo;
    cell.smallareaid.text = model.smallareaid;
    cell.v_houselayout.text = model.v_houselayout;
    cell.v_housesize.text = model.v_housesize;
    cell.v_price.text = [NSString stringWithFormat:@"%ld万", (long)model.v_price];
    return cell;
}
//跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    OldTwoViewController *oldTwo = [[OldTwoViewController alloc] init];
    OldModel *model = [self.array objectAtIndex:indexPath.row];
    oldTwo.hid_id = model.houseid;
    oldTwo.picURL = model.housepic;
    
    
    CATransition *trans = [CATransition animation];
    [self.navigationController pushViewController:oldTwo animated:NO];
    trans.type = @"rippleEffect";
    trans.duration = 0.8f;
    [self.navigationController.view.layer addAnimation:trans forKey:nil];
    
    [oldTwo release];
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    
    [self.navigationItem setHidesBackButton:YES];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    btn.frame = CGRectMake(15, 5, 38, 38);
    
    [btn setBackgroundImage:[UIImage imageNamed:@"返回.png"] forState:UIControlStateNormal];
    
    [btn addTarget: self action: @selector(goBackAction) forControlEvents: UIControlEventTouchUpInside];
    
    UIBarButtonItem*back=[[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem=back;
    
    
   [self createTable];
   [self getConnect];
    UIColor *ballColor = [UIColor colorWithRed:0.47 green:0.60 blue:0.89 alpha:1];
    self.pendulum = [[PendulumView alloc] initWithFrame:self.view.bounds ballColor:ballColor];
    [self.view addSubview:_pendulum];
    
    
}
-(void)goBackAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark -
#pragma mark 上拉下载下拉刷新
// 用来判断是上拉加载还是下啦刷新, state为当前加载状态
- (void)scrollView:(UIScrollView *)scrollView loadWithState:(LoadState)state
{
    NSLog(@"%u",state);
    if (state == PullDownLoadState) {
        // 判断为下拉刷新, 2秒后执行downLoad方法
        [self getConnect];
    } else {
        
        // 判断为上拉加载, 2秒后执行方法
        [self loadMore];
    }
}
//上拉加载,计数器self.type来判断是下拉还是上拉,以便于上面的停止
- (void)loadMore
{
    self.count++;
    self.type = 1;
    [self createNet];
}

// 下拉刷新
-(void)getConnect
{
    self.count = 1;
    self.type = 2;
    [self createNet];
}
- (void)createNet
{
    NSString *str = [NSString stringWithFormat:@"%ld", self.count];
    NSDictionary *dic = @{@"cityid":@"dl", @"page":str};
    self.conn = [ConnectModel connectWithUrl:@"http://m.goufang.com/api/ershou/eshonglist" delegate:self parmaters:dic];
}
@end
